// 学生类型定义
export interface Student {
  id: number;
  studentId: string;
  name: string;
  gender: string;
  age: number;
  major: string;
  class: string;
  admissionYear: string;
  phone: string;
  email: string;
  address: string;
  status: string;
}
export interface Classroom {
  id: number;
  name: string;
  location: string;
  capacity: number;
  type: string;
  available: boolean;
  equipment: string[];
}


// 从 localStorage 获取学生数据
export const getStudentsFromLocalStorage = (): Student[] => {
  try {
    const savedStudents = localStorage.getItem('students');
    if (savedStudents) {
      return JSON.parse(savedStudents);
    }
  } catch (error) {
    console.error('从localStorage加载学生数据失败:', error);
  }
  
  // 默认mock数据
  return [
    {
      id: 1,
      studentId: '2023001',
      name: '张三',
      gender: '男',
      age: 19,
      major: '计算机科学与技术',
      class: '计科1班',
      admissionYear: '2023',
      phone: '13800138001',
      email: 'zhangsan@example.com',
      address: '北京市海淀区',
      status: '在读'
    },
    {
      id: 2,
      studentId: '2023002',
      name: '李四',
      gender: '女',
      age: 18,
      major: '软件工程',
      class: '软件1班',
      admissionYear: '2023',
      phone: '13800138002',
      email: 'lisi@example.com',
      address: '上海市浦东新区',
      status: '在读'
    },
    // 更多学生数据...
  ];
};

// 从localStorage或mock获取教室数据
export const getClassrooms = (): Classroom[] => {
  try {
    // 首先尝试从localStorage获取数据
    const savedClassrooms = localStorage.getItem('classrooms');
    if (savedClassrooms) {
      return JSON.parse(savedClassrooms);
    }
    
    // 如果localStorage没有，从mock数据导入
    const { mockClassroomData } = require('../mock/classroomData');
    return mockClassroomData;
  } catch (error) {
    console.error('获取教室数据失败:', error);
    return [];
  }
};

// 获取仪表盘统计数据
export const getDashboardStats = () => {
  const students = getStudentsFromLocalStorage();
  const classrooms = getClassrooms();
  
  // 计算在读学生数量
  const activeStudents = students.filter(student => student.status === '在读').length;
  
  // 计算可用教室数量
  const availableClassrooms = classrooms.filter(classroom => classroom.available).length;
  
  return {
    totalStudents: students.length,
    activeStudents,
    totalClassrooms: classrooms.length,
    availableClassrooms,
    classrooms,
    students
  };
};

// 数据变化监听器管理
export class DashboardDataManager {
  private static instance: DashboardDataManager;
  private listeners: Array<() => void> = [];
  
  private constructor() {
    // 监听localStorage变化
    window.addEventListener('storage', this.handleStorageChange);
  }
  
  public static getInstance(): DashboardDataManager {
    if (!DashboardDataManager.instance) {
      DashboardDataManager.instance = new DashboardDataManager();
    }
    return DashboardDataManager.instance;
  }
  
  private handleStorageChange = (event: StorageEvent) => {
    if (event.key === 'students' || event.key === 'classrooms') {
      this.notifyListeners();
    }
  };
  
  // 添加数据变化监听器
  public addListener(listener: () => void): void {
    if (!this.listeners.includes(listener)) {
      this.listeners.push(listener);
    }
  }
  
  // 移除数据变化监听器
  public removeListener(listener: () => void): void {
    this.listeners = this.listeners.filter(l => l !== listener);
  }
  
  // 通知所有监听器数据已变化
  public notifyListeners(): void {
    this.listeners.forEach(listener => listener());
  }
}

// 导出数据管理器实例
export const dashboardDataManager = DashboardDataManager.getInstance();